package com.zb.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.AccounLoginInfo;
import com.zb.entity.ZjSystemLog;
import com.zb.entity.ZjSystemLogContent;
import com.zb.mapper.ZjSystemLogMapper;
import com.zb.result.PageUtil;
import com.zb.service.ZjSystemLogContentService;
import com.zb.service.ZjSystemLogService;
import com.zb.vo.SystemLogListSearchVo;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统日志表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-07-29
 */
@Service
public class ZjSystemLogServiceImpl extends ServiceImpl<ZjSystemLogMapper, ZjSystemLog> implements ZjSystemLogService {
    @Resource
    private ZjSystemLogMapper systemLogMapper;
    @Resource
    private ZjSystemLogContentService systemLogContentService;

    @Override
    public void saveSuccess(AccounLoginInfo logintInfo, Integer empType, String ip, String browserDevice, Integer moduleType, Integer type, String path, String remark, String param) {
        this.save(logintInfo, empType, ip, browserDevice, moduleType, type, path, true, remark, param, null);
    }

    @Override
    public void saveError(AccounLoginInfo logintInfo, Integer empType, String ip, String browserDevice, Integer moduleType, Integer type, String path, String remark, String param, String content) {
        this.save(logintInfo, empType, ip, browserDevice, moduleType, type, path, false, remark, param, content);
    }

    @Override
    public void save(AccounLoginInfo logintInfo, Integer empType, String ip, String browserDevice, Integer moduleType, Integer type, String path, boolean success, String remark, String param, String content) {
        if (null == logintInfo) {
            return;
        }
        ZjSystemLog log = new ZjSystemLog();
        log.setId(IdWorker.getId());
        log.setCustAccId(null == logintInfo.getCustAccId()? 0L : logintInfo.getCustAccId());
        log.setAccount(logintInfo.getPhone());
        log.setName(logintInfo.getName());
        log.setIp(ip);
        log.setBrowserDevice(browserDevice);
        log.setModuleType(moduleType);
        log.setType(type);
        log.setPath(path);
        log.setSuccess(success);
        log.setRemark(remark);
        this.save(log);
        systemLogContentService.save(log.getId(), log.getCustAccId(), param, content);
    }

    // 获取系统操作日志列表
    @Override
    public List<ZjSystemLog> getList(Long custAccId, SystemLogListSearchVo search) {
        PageUtil.setPageLimit(search);
        return systemLogMapper.getList(custAccId, search);
    }


    // 获取系统操作日志列表总记录数
    @Override
    public long getListCount(Long custAccId, SystemLogListSearchVo search) {
        return systemLogMapper.getListCount(custAccId, search);
    }

    // 根据系统日志ID获取日志操作内容
    @Override
    public Object getSysLogOperateContent(Long id) {
        ZjSystemLogContent logContent = systemLogContentService.getById(id);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("param", null != logContent? logContent.getParam() : "");
        resultMap.put("content", null != logContent? logContent.getContent() : "");
        return resultMap;
    }
}
